#ifndef _FINEGERPRINT_PROTOCOL_HERAD_H
#define _FINEGERPRINT_PROTOCOL_HERAD_H
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <vector>

using namespace std;

#define PACKET_FIX_LEN 9

class FingerprintProtocol
{
public:
    typedef struct
    {
        uint8_t head[2];
        uint8_t addr[4];
        uint8_t pid;
        uint8_t len[2];
        uint8_t data[1];
    } packet_t;

    enum
    {
        COMMAND_PACKET = 0x01, // 命令
        DATA_PACKET = 0x02,    // 数据
        ACK_PACKET = 0x07,     // 应答
        END_PACKET = 0x08,     // 最后一个包结束包
    };

    FingerprintProtocol(const vector<uint8_t> &packet);
    FingerprintProtocol();

    // 添加包头
    void addPacketHead(const uint16_t head = 0xef01);
    // 添加地址
    void addPacketAddr(uint32_t addr = 0xFFFFFFFF);
    // 添加包标识ID
    void addPacketID(uint8_t pid);
    // 添加包长度
    void addPacketLen(uint16_t len);
    // 添加包内容
    void addPacketContent(uint8_t *data, uint32_t size);
    // 添加包的校验和
    void addPacketSum(void);
    // 显示包
    void showPacket(void) const;

    uint16_t getPacketHead(void) const;
    uint32_t getPacketAddr(void) const;
    uint8_t getPacketID(void) const;
    uint16_t getPacketDataLen(void) const;
    uint8_t *getPacketData(void) const;
    uint16_t getPacketSum(void) const;

    bool isPacketError(void);

    vector<uint8_t> getPakcet(void) const;

    //========================================
    // 握手协议
    static FingerprintProtocol makeHandshakeProtocol(void);
    // 通过数据包生产协议
    static FingerprintProtocol fromProtocolPacket(const vector<uint8_t> &packet);
    // 生成探测数据包
    static FingerprintProtocol makeDetectImageProtocol(void);
    // 生成指纹特征图像
    static FingerprintProtocol makeImageFeatureProtocol(uint8_t bufferID);
    // 特征合成模板
    static FingerprintProtocol makeImageTemplateProtocol(void);
    // 存储模板 pos 模版号
    static FingerprintProtocol makeStoreTemplateProtocol(uint16_t pos);
    // 指纹比对 startPos起始位置，n 搜索个数
    static FingerprintProtocol makeSearchFingerPrintProtocol(uint16_t startPos = 0, uint16_t n = 50);
    // 删除指定指纹
    static FingerprintProtocol makeDeleteFingerPrintProtocol(uint16_t pos);
    // 清空指纹
    static FingerprintProtocol makeClearFingerPrintProtocol(void);

private:
    vector<uint8_t> packet;
};

#endif